package copyengine.ui.component.list.old
{
	import copyengine.ui.component.list.old.animation.ICEListCellRenderAnimation;
	import copyengine.ui.component.list.old.cellrender.ICEOldCellRender;
	import copyengine.ui.component.list.old.dataprovider.CESingleRowDataProvider;
	import copyengine.ui.component.list.old.dataprovider.ICEListDataProvider;
	import copyengine.utils.GeneralUtils;

	import flash.geom.Rectangle;

	public class CESingleRowListCore extends CEListCore
	{

		public function CESingleRowListCore(_displayGroupCount:int, _layoutDirection:String, _eachCellRenderWidth:int, _eachCellRenderHeight:int, _contentPadding:int)
		{
			super(_displayGroupCount, _layoutDirection, _eachCellRenderWidth, _eachCellRenderHeight, _contentPadding);
		}

		override protected function getCurrentFirstProviderDataGroupIndex():int
		{
			return cellRenderVector[0].cellIndex;
		}

		override protected function getDataProviderGroupLength():int
		{
			return (dataProvider as CESingleRowDataProvider).totalDataCount;
		}

		override protected function refreshCellRenderGroup(_cellRenderGroupIndex:int, _dataProviderGroupIndex:int):void
		{
			var cellRender:ICEOldCellRender = cellRenderVector[_cellRenderGroupIndex];
			cellRender.recycle();
			cellRender.cellIndex = _dataProviderGroupIndex;
			cellRender.setData((dataProvider as CESingleRowDataProvider).getDataByIndex(_dataProviderGroupIndex));
			cellRender.drawNow();
		}

		override protected function swapCellRenderGroup(_groupIndex1:int, _groupIndex2:int):void
		{
			if (_groupIndex1 != _groupIndex2)
			{
				var swapICECellRender:ICEOldCellRender = cellRenderVector[_groupIndex1];
				cellRenderVector[_groupIndex1] = cellRenderVector[_groupIndex2];
				cellRenderVector[_groupIndex2] = swapICECellRender;
			}
		}

		override protected function layoutAllCellRender():void
		{
			var cellRenderPos:int = -getFirstCellRenderGroupOffset();
			for (var i:int = 0; i <= displayGroupCount; i++)
			{
				var cellRender:ICEOldCellRender = cellRenderVector[i];
				if (layoutDirection == LAYOUT_HORIZONTAL)
				{
					cellRender.container.x = cellRenderPos;
					cellRenderPos += (eachCellRenderWidth + groupContentPadding);
				}
				else // layoutDirection == LAYOUT_VERTICAL
				{
					cellRender.container.y = cellRenderPos;
					cellRenderPos += (eachCellRenderHeight + groupContentPadding);
				}
			}
		}


		override protected function initializeAllCellRender(_cellRenderInstanceClass:Class, _cellRenderAnimationClass:Class):void
		{
			cellRenderVector = new Vector.<ICEOldCellRender>();
			for (var i:int = 0; i <= displayGroupCount; i++)
			{
				var cellRender:ICEOldCellRender = new _cellRenderInstanceClass();
				var cellRenderAnimation:ICEListCellRenderAnimation;
				if (_cellRenderAnimationClass != null)
				{
					cellRenderAnimation = new _cellRenderAnimationClass();
				}
				addChild(cellRender.container);
				cellRender.initialize(cellRenderAnimation);
				cellRenderVector.push(cellRender);
			}
		}

		override protected function disposeAllCellRender():void
		{
			for each (var cellRender:ICEOldCellRender in cellRenderVector)
			{
				cellRender.cellRenderDispose();
			}
			cellRenderVector = null;
		}

		override protected function initListCoreScrollRect():void
		{
			if (layoutDirection == LAYOUT_HORIZONTAL)
			{
				this.scrollRect = new Rectangle(0, 0, displayGroupCount * (eachCellRenderWidth + groupContentPadding) - groupContentPadding,
												eachCellRenderHeight);
			}
			else // layout == LAYOUT_VERTICAL
			{
				this.scrollRect = new Rectangle(0, 0, eachCellRenderWidth, displayGroupCount * (eachCellRenderHeight + groupContentPadding) - groupContentPadding);
			}
		}

		override protected function setDataProvider(_dataProvider:ICEListDataProvider):void
		{
			dataProvider = _dataProvider as CESingleRowDataProvider;
			if (dataProvider == null)
			{
				dataProvider = new CESingleRowDataProvider();
			}
		}

		override protected function disposeDataProvider():void
		{
			dataProvider.dispose();
			dataProvider = null;
		}


	}
}